package com.vitas.coin.vm

import android.graphics.Paint
import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import androidx.core.graphics.toColorInt
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.snap.base.bean.price.PriceDTO
import com.snap.base.vm.PriceVM
import com.snap.common.databinding.view.ViewBindingAdapter.gone
import com.snap.common.main.startWebAct
import com.snap.util.dp2px
import com.vitas.coin.BR
import com.vitas.coin.BuildConfig
import com.vitas.coin.R
import com.vitas.coin.databinding.ItemPriceBinding

class RyPriceVM : PriceVM() {
    override fun setLayoutId(): Int {
        return R.layout.item_price
    }

    override fun setVariableId(): Int {
        return BR.itemPrice
    }

    override fun onCustomDiffUpdate(prices: ArrayList<PriceDTO>): Boolean {
        if (prices.size < 4) {
            diffUpdateData(prices)
        } else {
            diffUpdateData(prices.subList(0, 3))
        }
        return true
    }

    override fun itemBindViewHolder(
        dataBinding: ViewDataBinding,
        pos: Int,
        item: PriceDTO,
        viewHolder: RecyclerView.ViewHolder
    ) {
        if (dataBinding !is ItemPriceBinding) return
        dataBinding.tvOld.apply {
            text = (item.oriPrice / 100f).toString()
            paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
        }
        dataBinding.tvPrice.apply {
            text = (item.realPrice / 100f).toString()
        }

        dataBinding.tvIcon.apply {
            gone(TextUtils.isEmpty(item.angleInfo))
            val gradientDrawable = GradientDrawable(
                GradientDrawable.Orientation.LEFT_RIGHT,
                intArrayOf("#FE7059".toColorInt(), "#FF1739".toColorInt()),
            )
            gradientDrawable.cornerRadii = floatArrayOf(
                0f, 0f,
                12f.dp2px(), 12f.dp2px(),
                0f, 0f,
                12f.dp2px(), 12f.dp2px(),
            )
            this.background = gradientDrawable
        }
    }

    fun clickUrl() {
        startWebAct(BuildConfig.app_member, title = "会员服务协议", isGoneActionBar = false)
    }
}